package tnt.sudoku_client_android;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import message.LoginMessage;
import message.Message;
import message.QuitMessage;

import socket.handler.ClientSocketHandler;
import socket.handler.SocketHandler;
import socket.receiver.ClientReceiver;
import util.ConnectUtil;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity{
	private EditText username;
	private EditText password;
	private SocketHandler handler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		try {
			handler=ConnectUtil.getHandler(this);		
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		}		
		setContentView(R.layout.test);
		username=(EditText) findViewById(R.id.test_username);
		username.setText("noi");
		password=(EditText)findViewById(R.id.test_password);
		
	}
	public void login(View view){
		String u=username.getEditableText().toString();
		String p=password.getEditableText().toString();
		LoginMessage login=new LoginMessage();
		login.setType(Message.MESSAGE_LOGIN);
		login.setStatus(Message.REQUEST);
		login.setUsername(u);
		login.setPassword(p);
		Log.e("TEST",login.getType());
		handler.writeSocket(login);			
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK){
			QuitMessage quit=new QuitMessage();
			quit.setType(Message.MESSAGE_QUIT);
			quit.setStatus(Message.OK);
			handler.writeSocket(quit);
		}
		return super.onKeyDown(keyCode, event);
	}

}
